var e = require("../../../../@babel/runtime/helpers/interopRequireDefault").default, t = require("../../../../@babel/runtime/helpers/objectSpread2"), r = require("../../../../@babel/runtime/helpers/defineProperty"), a = e(require("@vant/weapp/dialog/dialog.js")), o = e(require("./activityRuleText")), i = e(require("../../../../api/api_queryRemain")), n = e(require("../../../../behaviors/auth.js")), s = e(require("../../../../utils/Tips"));

Component({
    behaviors: [ n.default ],
    properties: {
        query: {
            type: Object,
            default: {}
        }
    },
    data: {
        activityRuleText: o.default,
        showTokenGuide: !1,
        formItems: [ {
            prop: "password",
            label: "兑奖口令",
            type: "input",
            value: "",
            errorMsg: "",
            placeholder: "请输入您的兑奖口令"
        } ],
        form: {
            tag: "Q_COIN",
            password: ""
        },
        formRules: {
            password: [ {
                required: !0,
                message: "请输入您的兑奖口令"
            }, {
                patternStr: "^[A-Z|a-z|0-9]{8}$",
                message: "请输入正确的8位兑奖口令"
            } ]
        }
    },
    methods: {
        onInputChange: function(e) {
            var t = e.detail.value, a = e.target.dataset.prop;
            this.setData(r({}, "form.".concat(a), t));
        },
        clearValidateMsgs: function() {
            var e = this;
            this.data.formItems.forEach(function(t, a) {
                e.setData(r({}, "formItems[".concat(a, "].errorMsg"), ""));
            });
        },
        valiateProp: function() {
            var e = this, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "", a = this.data, o = a.formItems, i = a.formRules, n = a.form, s = i[t], l = {
                validate: !0,
                error: ""
            }, u = o.findIndex(function(e) {
                return e.prop === t;
            }), d = n[t], f = "formItems[".concat(u, "].errorMsg");
            if (Array.isArray(s)) {
                var c = function() {
                    for (var t = 0; t < s.length; t++) {
                        var r = s[t];
                        if (r.required && !d) return void (l = {
                            validate: !1,
                            error: r.message
                        });
                        if (r.patternStr) if (!new RegExp(r.patternStr).test(d)) return void (l = {
                            validate: !1,
                            error: r.message
                        });
                        if (r.validator) {
                            var a = r.validator.call(e, d, r);
                            if (a instanceof Error) return void (l = {
                                validate: !1,
                                error: a.message
                            });
                        }
                    }
                };
                c();
            }
            return l.validate ? this.setData(r({}, f, "")) : this.setData(r({}, f, l.error)), 
            l;
        },
        validateAll: function() {
            for (var e = Object.keys(this.data.formRules), t = [], r = !1, a = 0; a < e.length; a++) {
                var o = e[a], i = this.valiateProp(o);
                i.validate || (r = !0, t.push({
                    error: i.error
                }));
            }
            return {
                validate: !r,
                errors: t
            };
        },
        onInputBlur: function(e) {
            var t = e.target.dataset.index;
            this.valiateProp(this.data.formItems[t].prop);
        },
        handlePasswordNoteClick: function() {
            this.setData({
                showTokenGuide: !0
            });
        },
        onTokenGuideClose: function() {
            this.setData({
                showTokenGuide: !1
            });
        },
        beforeSubmitConfirm: function(e) {
            var t = e.password;
            return a.default.confirm({
                context: this,
                title: "温馨提示",
                confirmButtonText: "确认提交",
                richTextMessage: !0,
                customStyle: "width: auto;",
                richText: [ {
                    name: "div",
                    attrs: {
                        style: "margin-bottom:15px;text-align:left;white-space:nowrap;"
                    },
                    children: [ {
                        type: "text",
                        text: "确认提交后无法修改，请确认信息无误"
                    } ]
                }, {
                    name: "div",
                    attrs: {
                        style: "text-align:left;white-space:nowrap;"
                    },
                    children: [ {
                        type: "text",
                        text: "您的兑奖口令："
                    }, {
                        name: "span",
                        attrs: {
                            style: "color:#FD6D39;"
                        },
                        children: [ {
                            type: "text",
                            text: t
                        } ]
                    } ]
                } ]
            });
        },
        handleSubmit: function() {
            var e = this;
            this.validateAll().validate && this.beforeSubmitConfirm(this.data.form).then(function() {
                return s.default.loading(), i.default.submitLotteryForm(t(t({}, e.data.form), {}, {
                    roomId: e.data.query.roomId
                }));
            }).then(function(t) {
                s.default.loaded(), "10002" === t.code ? a.default.confirm({
                    context: e,
                    message: "您还未登录“王卡助手”小程序，请登录后提交",
                    confirmButtonOpenType: "getPhoneNumber",
                    confirmButtonText: "立即登录"
                }) : "0" === t.code ? (e.setData({
                    "form.accountNo": "",
                    "form.password": ""
                }), a.default.alert({
                    context: e,
                    message: "提交成功"
                })) : a.default.alert({
                    context: e,
                    message: t.message
                });
            }).catch(function(e) {
                console.log(e), s.default.loaded();
            });
        }
    }
});